using AppToolKit.Core.Context;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text.RegularExpressions;
namespace AppToolKit.Core.Utils
{
	public class DynamicExecuteor
	{
		public IContextManager ContextMgr
		{
			private get;
			set;
		}
		public object Eval(string expression)
		{
			string pattern = "(?<method>[^(]*)(\\((?<param>.*)\\))?,(?<assembly>.*)";
			Regex regex = new Regex(pattern, RegexOptions.Compiled | RegexOptions.Singleline);
			Match match = regex.Match(expression);
			if (!match.Success)
			{
				throw new Exception("expression invaild");
			}
			if (match.Groups["method"].Success)
			{
				string value = match.Groups["method"].Value;
				string value2 = match.Groups["assembly"].Value;
				object[] parameters = null;
				if (match.Groups["param"].Success)
				{
					List<object> list = new List<object>();
					string value3 = match.Groups["param"].Value;
					string[] array = value3.Split(new char[]
					{
						','
					});
					string text = null;
					string[] array2 = array;
					for (int i = 0; i < array2.Length; i++)
					{
						string text2 = array2[i];
						if (text2.StartsWith("\"") || text2.StartsWith("'"))
						{
							text = text2.Substring(1, text2.Length - 2);
							DateTime dateTime;
							if (DateTime.TryParse(text, out dateTime))
							{
								list.Add(dateTime);
							}
							else
							{
								list.Add(text);
							}
						}
						else
						{
							decimal num;
							if (decimal.TryParse(text, out num))
							{
								list.Add(num);
							}
							else
							{
								int num2;
								if (int.TryParse(text, out num2))
								{
									list.Add(num2);
								}
								else
								{
									double num3;
									if (double.TryParse(text, out num3))
									{
										list.Add(num3);
									}
								}
							}
						}
					}
					parameters = list.ToArray();
				}
				int num4 = value.LastIndexOf(".");
				string arg = value.Substring(0, num4);
				string name = value.Substring(num4 + 1);
				Type type = Type.GetType(string.Format("{0},{1}", arg, value2), true);
				object obj = this.ContextMgr.GetObject(type);
				if (obj == null)
				{
					obj = this.ContextMgr.Create(type);
				}
				MethodInfo method = type.GetMethod(name);
				return method.Invoke(obj, parameters);
			}
			throw new Exception("evel 无法找到 method name");
		}
	}
}
